package View;

import Bussiness.Insert_Action;
import Bussiness.Search_Action;
import Bussiness.Update_Action;
import Model.SinhVien;

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.SystemColor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;

/**
 * 
 * @author Nhóm java lớp PNV2T - Nguyễn Thị Lệ Đài - Nguyễn Chí Hướng
 * 
 */
public class SuaSinhVien extends javax.swing.JFrame {
	private SinhVien sv = null;

	/** Creates new form SuaSinhVien */
	public SuaSinhVien(String MaSV) {
		setAlwaysOnTop(true);
		setResizable(false);
		try {
			UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		} catch (UnsupportedLookAndFeelException ex) {
			ex.printStackTrace();
		} catch (IllegalAccessException ex) {
			ex.printStackTrace();
		} catch (InstantiationException ex) {
			ex.printStackTrace();
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		}
		initComponents();

		setLocation(550, 10);

		Search_Action sa = new Search_Action();
		lbl_MaSVGanNhat.setText(sa.MaSVGanNhat());
		sv = sa.getSinhVien(MaSV);
		sa.Close();

		txt_MaSV.setText(sv.getMaSV());
		txt_TenSV.setText(sv.getTen());
		txt_QueQuan.setText(sv.getQueQuan());
		txt_NgaySinh.setText(sv.getNgaySinh());
		txt_TonGiao.setText(sv.getTonGiao());
		System.out.println("gioi tinh " + sv.getGioiTinh());
		if ("Nam".equalsIgnoreCase(sv.getGioiTinh().trim())) {
			rdBtn_Nam.setSelected(true);
		} else {
			rdBtn_Nu.setSelected(true);
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed"
	// desc=" Generated Code ">//GEN-BEGIN:initComponents
	private void initComponents() {
		jPanel1 = new javax.swing.JPanel();
		jToolBar1 = new javax.swing.JToolBar();
		btn_CapNhap = new javax.swing.JButton();
		jSeparator1 = new javax.swing.JSeparator();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel5 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel7 = new javax.swing.JLabel();
		txt_MaSV = new javax.swing.JTextField();
		txt_NgaySinh = new javax.swing.JTextField();
		jLabel8 = new javax.swing.JLabel();
		jLabel9 = new javax.swing.JLabel();
		txt_TenSV = new javax.swing.JTextField();
		txt_QueQuan = new javax.swing.JTextField();
		rdBtn_Nam = new javax.swing.JRadioButton();
		rdBtn_Nu = new javax.swing.JRadioButton();
		jLabel11 = new javax.swing.JLabel();
		txt_TonGiao = new javax.swing.JTextField();
		jLabel10 = new javax.swing.JLabel();
		jLabel12 = new javax.swing.JLabel();
		lbl_MaSVGanNhat = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Sửa thông tin sinh viên ...");
		jPanel1.setBackground(SystemColor.activeCaption);
		jToolBar1.setBackground(SystemColor.inactiveCaption);
		jToolBar1.setFloatable(false);

		btn_CapNhap.setBackground(new java.awt.Color(227, 239, 249));
		btn_CapNhap.setFont(new java.awt.Font("Times New Roman", 1, 13));
		btn_CapNhap.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/Icon/Sua.png")));
		btn_CapNhap.setText("Cập nhật");
		btn_CapNhap.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btn_CapNhapActionPerformed(evt);
			}
		});

		jToolBar1.add(btn_CapNhap);

		jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel3.setText("Mã SV");

		jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel4.setText("Tên SV :");

		jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel5.setText("Quê Quán");

		jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel6.setText("Ngày Sinh :");

		jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel7.setText("Giới Tính :");

		txt_MaSV.setEditable(false);
		txt_MaSV.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_MaSV.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		txt_NgaySinh.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_NgaySinh.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		jLabel8.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel8.setForeground(new java.awt.Color(204, 0, 0));
		jLabel8.setText("(*)");

		jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel9.setForeground(new java.awt.Color(204, 0, 0));
		jLabel9.setText("(*)");

		txt_TenSV.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_TenSV.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		txt_QueQuan.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_QueQuan.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		rdBtn_Nam.setFont(new java.awt.Font("Times New Roman", 1, 13));
		rdBtn_Nam.setText("Nam");
		rdBtn_Nam.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0,
				0, 0));
		rdBtn_Nam.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rdBtn_Nam.setOpaque(false);

		rdBtn_Nu.setFont(new java.awt.Font("Times New Roman", 1, 13));
		rdBtn_Nu.setText("Nữ");
		rdBtn_Nu.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0,
				0));
		rdBtn_Nu.setMargin(new java.awt.Insets(0, 0, 0, 0));
		rdBtn_Nu.setOpaque(false);

		ButtonGroup rdGender = new ButtonGroup();
		rdGender.add(rdBtn_Nam);
		rdGender.add(rdBtn_Nu);

		jLabel11.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel11.setText("Tôn Giáo :");

		txt_TonGiao.setFont(new java.awt.Font("Times New Roman", 0, 13));
		txt_TonGiao.setBorder(javax.swing.BorderFactory
				.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

		jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel10.setText("( Tháng/Ngày/Năm )");

		jLabel12.setFont(new java.awt.Font("Times New Roman", 0, 13));
		jLabel12.setText("Mã SV gần nhất: ");

		lbl_MaSVGanNhat.setFont(new java.awt.Font("Times New Roman", 1, 13));
		lbl_MaSVGanNhat.setForeground(new java.awt.Color(255, 51, 51));
		lbl_MaSVGanNhat.setText("MaSV");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel5)
														.addComponent(jLabel3)
														.addComponent(jLabel4)
														.addComponent(jLabel6)
														.addComponent(jLabel7)
														.addComponent(jLabel11))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																txt_TonGiao,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																191,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				rdBtn_Nam)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				rdBtn_Nu))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																						.addComponent(
																								txt_MaSV,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								191,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								txt_TenSV,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								191,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								txt_QueQuan,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								191,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				jPanel1Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								jLabel9)
																						.addComponent(
																								jLabel8)))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				txt_NgaySinh,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				92,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jLabel10)))
										.addContainerGap())
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel12)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(lbl_MaSVGanNhat)
										.addContainerGap(159, Short.MAX_VALUE))
						.addComponent(jSeparator1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 304,
								Short.MAX_VALUE)
						.addComponent(jToolBar1,
								javax.swing.GroupLayout.DEFAULT_SIZE, 304,
								Short.MAX_VALUE));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addComponent(
												jToolBar1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												38,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(
												jSeparator1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												2,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel12)
														.addComponent(
																lbl_MaSVGanNhat))
										.addGap(9, 9, 9)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel3)
														.addComponent(
																txt_MaSV,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel8))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel4)
														.addComponent(jLabel9)
														.addComponent(
																txt_TenSV,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel5)
														.addComponent(
																txt_QueQuan,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel6)
														.addComponent(
																txt_NgaySinh,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(jLabel10))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel7)
														.addComponent(rdBtn_Nam)
														.addComponent(rdBtn_Nu))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel11)
														.addComponent(
																txt_TonGiao,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																24,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.PREFERRED_SIZE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.PREFERRED_SIZE));
		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void btn_CapNhapActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_btn_CapNhapActionPerformed
		String TenSV = txt_TenSV.getText();
		String QueQuan = txt_QueQuan.getText();
		String NgaySinh = txt_NgaySinh.getText();
		String GioiTinh = "";
		if (rdBtn_Nam.isSelected() == true)
			GioiTinh = "Nam";
		if (rdBtn_Nu.isSelected() == true)
			GioiTinh = "Nữ";
		String TonGiao = txt_TonGiao.getText();

		if (TenSV.equalsIgnoreCase("")) {
			JOptionPane.showMessageDialog(null, "Chưa nhập tên sinh viên!",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
			txt_TenSV.requestFocus();
		} else if (NgaySinh.equalsIgnoreCase("")) {
			JOptionPane.showMessageDialog(null,
					"Nhập ngày sinh đúng định dạng!", "Null Values !",
					JOptionPane.ERROR_MESSAGE);
			txt_NgaySinh.requestFocus();
		} else if (GioiTinh.equalsIgnoreCase(""))
			JOptionPane.showMessageDialog(null, "Hãy chọn giới tính!",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
		else {
			sv.setTen(TenSV);
			sv.setQueQuan(QueQuan);
			sv.setNgaySinh(NgaySinh);
			sv.setGioiTinh(GioiTinh);
			sv.setTonGiao(TonGiao);

			Update_Action ua = new Update_Action();
			if (ua.SuaSinhVien(sv) == true) {
				int j = JOptionPane.showConfirmDialog(null,
						"Sửa thành công, bạn có muốn tiếp tục không?",
						"Continued ... !", 2);
				if (j != 0)
					this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "Khong them duoc !");
			}
			ua.Close();
		}
		try {
			QuanLy.loadDataStudent();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}// GEN-LAST:event_btn_CapNhapActionPerformed

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton btn_CapNhap;
	private javax.swing.JLabel jLabel10;
	private javax.swing.JLabel jLabel11;
	private javax.swing.JLabel jLabel12;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;
	private javax.swing.JLabel jLabel5;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JLabel jLabel7;
	private javax.swing.JLabel jLabel8;
	private javax.swing.JLabel jLabel9;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JToolBar jToolBar1;
	private javax.swing.JLabel lbl_MaSVGanNhat;
	private javax.swing.JRadioButton rdBtn_Nam;
	private javax.swing.JRadioButton rdBtn_Nu;
	private javax.swing.JTextField txt_MaSV;
	private javax.swing.JTextField txt_NgaySinh;
	private javax.swing.JTextField txt_QueQuan;
	private javax.swing.JTextField txt_TenSV;
	private javax.swing.JTextField txt_TonGiao;
}
